import 'dart:io';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/viewmodel/meal/meal_recorder_view_model.dart';

class MealRecordPage extends BaseStatefulMvvmWidget{
  final ElderInfo? info;

  MealRecordPage({this.info});

  @override
  State<StatefulWidget> createState() => _MealRecordState();

}

class _MealRecordState extends BaseMvvmListState<MealRecorderViewModel,MealRecordPage>{
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            finish();
          },
          icon: Icon(
              Platform.isAndroid ? Icons.arrow_back : Icons.arrow_back_ios),
        ),
        title: Text("饮食登记"),
        actions: [
          IconButton(
              onPressed: () {
                key.currentState?.openEndDrawer();
              },
              icon: Icon(Icons.search))
        ],
      ),
      endDrawer: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AppBar(
            leading: null,
            automaticallyImplyLeading: false,
            title: Text("筛选"),
            actions: [Container()],
          ),
         widget.info!=null?Container(): CommonText("长者姓名")
              .addToContainer(margin: EdgeInsets.only(left: 16, top: 10)),
          widget.info!=null?Container(): CommonInput(
            hintText: "输入长者姓名",
            border: InputBorder.none,
            padding: EdgeInsets.zero,
            isDense: true,
            textInputAction: TextInputAction.search,
            onChanged: (str) {
              viewModel.elderNameLike = str;
            },
            onSubmitted: (str) {
              viewModel.requestRefresh();
            },
          ).addToContainer(
              decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.circular(8)),
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
              padding: EdgeInsets.all(8)),

          IconTitleTextWidget(
            "点餐时间",
            text: viewModel.startDateStr,
            hintText: "选择点餐时间",
          ).onTap(() {
            viewModel.selectStartDate(
                max: DateTime.now(),
                click: (date) {
                  viewModel.requestRefresh();
                });
          }),

          Container().setWeight(1),
          Gaps.line,
          Row(
            children: [
              TextButton(
                onPressed: () {finish();
                },
                child: CommonText("关闭"),
                style: ButtonStyle(
                    minimumSize:
                    WidgetStateProperty.all(Size.fromHeight(50))),
              ).setWeight(1),
              TextButton(
                onPressed: () {
                  finish();
                  viewModel.requestRefresh();
                  },
                child: CommonText("确定", textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor:
                    WidgetStateProperty.all(Colours.app_main),
                    minimumSize:
                    WidgetStateProperty.all(Size.fromHeight(50))),
              ).setWeight(1)
            ],
          )
        ],
      ).addToContainer(
        color: Colors.white,
        width: 300,
      ),
      body: Stack(
        children: [
          loadingContentWidget.setLocation(
              left: 0, right: 0, top: 0, bottom: 66),
          ElevatedButton(
            onPressed: () {
              viewModel.toAddSign();
            },
            child: CommonText("新增", textColor: Colors.white, textSize: 17),
            style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Color(0xfff6ab00)),
                minimumSize: WidgetStateProperty.all(Size.fromHeight(50))),
          ).setLocation(left: 16, right: 16, bottom: 16)
        ],
      ),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    // var item = viewModel.listItems[index];
    var date = DateTime.parse(item.bean?.createdDate??"");
    var dateFormat = [yyyy,"年",mm,"月",dd,"日"," ",D];
    var timeFormat = [HH,":",nn];
    return Column(
      children: [
        CommonText("${formatDate(date, dateFormat, LocaleType.zh)}",
            textSize: 12, textColor: Color(0xff666666))
            .addToContainer(margin: EdgeInsets.symmetric(vertical: 15)),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CommonText("${formatDate(date, timeFormat, LocaleType.zh)}", textColor: Color(0xff999999))
                .addToContainer(margin: EdgeInsets.only(top: 3)),
            Container(
              width: 18,
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    CommonText("${item.bean?.elderName??""}", fontWeight: FontWeight.bold),
                    Container(
                      width: 12,
                    ),
                    CommonText("${item.bean?.roomNo??""}", textSize: 14)
                  ],
                ),
                Container(
                  height: 8,
                ),
                CommonText("记录人：${item.caption?.createdBy??""}", textSize: 14),
                Container(
                  height: 8,
                ),
                CommonText("菜品名称：${(item.bean?.details??[]).map((e) => e.menuName).toList().join("、")}", textSize: 14),
                Container(
                  height: 15,
                ),
              ],
            )
          ],
        ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16))
      ],
    ).addToContainer(color: Colors.white);
  }

  @override
  Widget get separatorDivider => Divider(height: 1,color: Colors.grey,);

  @override
  MealRecorderViewModel createViewModel() {
    return MealRecorderViewModel(context,info: widget.info);
  }

}